package com.winsdom.controller;


import com.winsdom.anno.Authentication;
import com.winsdom.entity.BillingMain;
import com.winsdom.entity.count.BillingDetailsPageResult;
import com.winsdom.entity.count.BillingDetailsQueryRequest;
import com.winsdom.entity.count.BillingDetailsRequest;
import com.winsdom.entity.count.ManualPostingRequest;
import com.winsdom.entity.customer.ResponseResult;
import com.winsdom.result.Result;
import com.winsdom.service.IBillingMainService;
import com.winsdom.service.count.BillingDetailsService;
import com.winsdom.service.count.ManualPostingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.winsdom.enums.AuthenticationType.CUSTOMER_MANAGEMENT;

/**
 * <p>
 * 计费主表 前端控制器
 * </p>
 *
 * @author 
 * @since 2025-05-22
 */
@RestController
@RequestMapping("/billing-main")
@Api(tags = "计费管理")
@Authentication(value=CUSTOMER_MANAGEMENT)
public class BillingMainController {
    @Autowired
    private IBillingMainService  billingMainService;
    @GetMapping("/autoAdd")
    @ApiOperation("设置定时任务自动获取到期计费记录，并添加到计费主表")
    //每天0点0分执行
    @Scheduled(cron = "0 * * * * ?")
    public Result autoAdd(){
        billingMainService.autoAdd();
        return Result.success("更新成功");
    }

    @Autowired
    private ManualPostingService manualPostingService;

    @PostMapping("/generate")
    @ApiOperation("手动入账")
    public ResponseResult<Void> generateBills(@RequestBody ManualPostingRequest request) {
        try {
            manualPostingService.generateBills(request);
            return ResponseResult.success(null);
        } catch (Exception e) {
            return ResponseResult.error("账单生成失败：" + e.getMessage());
        }
    }

    @Autowired
    private BillingDetailsService billingDetailsService;

    @PostMapping("/list")
    @ApiOperation("分页查询计费明细列表")
    public ResponseResult<BillingDetailsPageResult> list(@RequestBody BillingDetailsRequest request) {
        try {
            BillingDetailsPageResult pageResult = billingDetailsService.getBillingDetailsList(
                    request.getPageNum(),
                    request.getPageSize()
            );
            return ResponseResult.success(pageResult);
        } catch (Exception e) {
            return ResponseResult.error("查询计费明细列表失败：" + e.getMessage());
        }
    }

    @PostMapping("/search")
    @ApiOperation("条件查询计费明细列表")
    public ResponseResult<BillingDetailsPageResult> search(@RequestBody BillingDetailsQueryRequest request) {
        try {
            BillingDetailsPageResult pageResult = billingDetailsService.getBillingDetailsWithCondition(request);
            return ResponseResult.success(pageResult);
        } catch (Exception e) {
            return ResponseResult.error("条件查询计费明细列表失败：" + e.getMessage());
        }
    }
}
